package com.stylefeng.guns.modular.doorCars.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.modular.doorDevice.service.IDeviceService;
import com.stylefeng.guns.modular.system.model.Device;
import com.stylefeng.guns.modular.tool.Tool;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.Car;
import com.stylefeng.guns.modular.doorCars.service.ICarService;

import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.List;

/**
 * 卡片管理控制器
 *
 * @author fengshuonan
 * @Date 2018-09-28 17:07:27
 */
@Controller
@RequestMapping("/car")
public class CarController extends BaseController {

    private String PREFIX = "/doorCars/car/";

    @Autowired
    private ICarService carService;
    @Autowired
    private IDeviceService deviceService;

    /**
     * 跳转到卡片管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "car.html";
    }

    /**
     * 跳转到添加卡片管理
     */
    @RequestMapping("/car_add")
    public String carAdd() {
        return PREFIX + "car_add.html";
    }

    /**
     * 跳转到修改卡片管理
     */
    @RequestMapping("/car_update/{carId}")
    public String carUpdate(@PathVariable Integer carId, Model model) {
        Car car = carService.selectById(carId);
        model.addAttribute("item",car);
        LogObjectHolder.me().set(car);
        return PREFIX + "car_edit.html";
    }

    /**
     * 获取卡片管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        if(condition!=null) {
            EntityWrapper<Car> wrapper = new EntityWrapper<Car>();
            wrapper.like("department", condition);
            return carService.selectList(wrapper);
        }
        return carService.selectList(null);
    }

    /**
     * 新增卡片管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Car car,String model) {
        car.setModel(model);
        carService.insert(car);
        return SUCCESS_TIP;
    }

    /**
     * 删除卡片管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer carId) {
        Car car = carService.selectById(carId);
        String model=car.getModel();
        List list1=new ArrayList();
        List list2=new ArrayList();
        List list=new ArrayList();
        List list3=new ArrayList();
        List list4=new ArrayList();
        List list5=new ArrayList();
        String[] models = model.split(",");
        for (int t = 0; t < models.length; t++) {
            list1.add(models[t]);
        }
for (int i=0;i<list1.size();i++){
    EntityWrapper<Device> wrapper = new EntityWrapper<Device>();
    wrapper.like("name", (String) list1.get(i));
       List<Device> device=deviceService.selectList(wrapper);

       for (int j=0;j<device.size();j++){
           list2.add(device.get(j).getIp()) ;
           list3.add(device.get(j).getAccount());
           list4.add(device.get(j).getPassword());
           list5.add(device.get(j).getPort());
       }
        }
        list.add(car.getDepartment());
        list.add(car.getStarttime());
        list.add(car.getStoptime());
        Tool.delectCar(list2,list,list3,list4,list5);
        carService.deleteById(carId);
        return SUCCESS_TIP;
    }

    /**
     * 修改卡片管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Car car) {
        carService.updateById(car);
        return SUCCESS_TIP;
    }

    /**
     * 卡片管理详情
     */
    @RequestMapping(value = "/detail/{carId}")
    @ResponseBody
    public Object detail(@PathVariable("carId") Integer carId) {
        return carService.selectById(carId);
    }
}
